﻿using UnityEngine;
using System.Collections;

public class enemySI : MonoBehaviour {

	private MeshRenderer render;
	public enemySI previousLine;
	public enemySI nextLine;
	public bool moved = false;
	private Vector3 dir = Vector3.left;
	private Vector3 lastDir;
	private Transform ownTransform;
	private float time = 0;
	public float speed = 10;
	private float MaxTime = 0.5f;
	private Camera camara;
	private bool down = false;
	public bool first = false;
	// Use this for initialization
	void Start () {
		render = (gameObject.GetComponent ("MeshRenderer") as MeshRenderer);
		ownTransform = transform;
		camara = GameObject.FindWithTag ("MainCamera").GetComponent ("Camera") as Camera;
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if (previousLine.getMoved () && !moved) {
			time += Time.fixedDeltaTime;
			if(time > MaxTime){
				checkDir ();
				if(first){
					ownTransform.position = ownTransform.position + dir*Time.fixedDeltaTime*speed;
				}else{
					ownTransform.position = previousLine.transform.position + new Vector3(0, 5, 0);
				}
				moved = true;
				nextLine.setMoved (false);
				nextLine.resetTime ();
			}
		}
	}

	public bool getMoved(){
		return moved;
	}

	public void setMoved(bool mov){
		moved = mov;
	}

	public void resetTime(){
		time = 0;
	}

	public void setMaxTime(float num){
		MaxTime = num;
	}

	void checkDir(){
		if (down) {
			lastDir = dir;
			dir = Vector3.down;
			down = false;
		} else if (lastDir == Vector3.right) {
			dir = Vector3.left;
		} else if(lastDir == Vector3.left) {
			dir = Vector3.right;
		}
	}

	public void setDown(bool D){
		down = D;
	}

	public bool getDown(){
		return down;
	}
}
